let isReady = false;
const listeners = [];
const keys = {};

export default {
    ready(callback, key) {
        if (key) {
            if (keys[key]) return;
            keys[key] = true;
        }
        if (isReady) {
            callback();
        } else {
            listeners.push(callback);
        }
    },
    start() {
        isReady = false;
    },
    end() {
        isReady = true;
        if (listeners.length > 0) {
            while (listeners.length > 0) {
                const listener = listeners.pop();
                listener();
            }
        }
    },
    isReady() {
        return isReady;
    },
};
